from django.shortcuts import render

# Create your views here.
from django.views import View

import upyun
from django.db import models
from django.core.files.storage import Storage
from django.utils.six.moves.urllib.parse import urljoin
from django.utils.encoding import filepath_to_uri


class UpyunStorage(Storage,View):

    BUCKETNAME = 'xxxx'
    USERNAME = 'the5fire'
    PASSWORD = 'xxxxxx'
    BASE_URL = "http://xxxxx.b0.upaiyun.com"
    up = upyun.UpYun(BUCKETNAME, USERNAME, PASSWORD, timeout=30,
                                        endpoint=upyun.ED_AUTO)

    def _save(self, name, content):
        full_url = self.BASE_URL  + name
        try:
            res = self.up.put(name, content.read(), checksum=False)
        except Exception as e:
            raise

        return full_url

    def exists(self, name):
        try:
            self.up.getinfo(name)
        except Exception:
            return False
        return True

    def listdir(self, path):
        pass

    def size(self, name):
        return 0

    def url(self, name):
        return urljoin(self.BASE_URL, filepath_to_uri(name))


    def get(self,request):
        return render(request,'test/model_lsit.html')

